package com.cuit.dao;

import com.cuit.domain.User;
import com.cuit.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDao {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

    public int login(User user) {
        String sql = "select * from user where username = ? and password = ?";
        Object[] params = {user.getUsername(), user.getPassword()};
        User userCheck = null;
        try {
            userCheck = queryRunner.query(sql, new BeanHandler<>(User.class), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (userCheck != null) {
            return 1;
        }
        return 0;
    }

    public int register(User user) {
        String sql = "insert into user values(null, ?, ?)";
        Object[] params = {user.getUsername(), user.getPassword()};
        try {
            return queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
